package com.interauto.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.interauto.bean.web.CheckInfoWeb;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AutoStringUtils {
    public static boolean isNumeric(String str) {
        String pattern = "^[\\+\\-]?[\\d]+(\\.[\\d]+)?$";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);
        return m.matches();
    }

    /**
     * 将json转成map
     * @param str 要转换json
     * @return 对应的map
     */
    public static Map<String,String> getMaptoJson(String str){
        if(StringUtils.isEmpty(str)){
            return null;
        }
        Map<String,String> headmap=new HashMap<>();
        try {
            JSONObject jsonObject = JSON.parseObject(str);
            for(Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                headmap.put(entry.getKey(),String.valueOf(entry.getValue()));
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return headmap;
    }

    /**
     * 将对象转成json
     * @param mapobj 要转的对象
     * @return
     */
    public static String getJsontoMap(Object mapobj){
        if(mapobj == null){
            return "";
        }
        try{
            return JSON.toJSONString(mapobj);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }



    public static void main(String[] args) throws Exception{
        String str="[{\"autoExpectEnum\":\"AssertEquals\",\"checkinfo\":\"$.code\",\"expectinfo\":\"200\"},{\"autoExpectEnum\":\"AssertContains\",\"checkinfo\":\"$.data\",\"expectinfo\":\"BX11P\"}]";
        //JSONObject jsonObject=JSONObject.parseObject(str);
        //List<CheckInfoWeb> checkInfoWebList=JSON.parseObject(str,new TypeReference<List<CheckInfoWeb>>(){});
        System.out.println(isNumeric("123"));
    }
}
